package com.h2View.controller;

import com.h2View.db.dao.SqlMapper;
import com.h2View.utils.MybatisUtils;
import com.h2View.utils.StringUtil;
import com.xjw.annotation.FXController;
import com.xjw.annotation.FXRedirect;
import com.xjw.annotation.FXWindow;
import com.xjw.entity.FXBaseController;
import com.h2View.utils.ToolUtil;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableView;
import javafx.scene.control.TextArea;
import javafx.scene.input.KeyCode;
import javafx.scene.text.Text;

import java.net.URL;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.ResourceBundle;

/**
 * @author 徐军文
 * @since 2021/7/3 13:24
 */
@FXController(path = "fxml/test.fxml")
@FXWindow(title = "执行sql", icon = "image/icon.png")
public class MainController extends FXBaseController implements Initializable {

    @FXML
    private TextArea sqlEle;

    @FXML
    private TextArea resultEle;

    @FXML
    private TableView<LinkedHashMap<String,Object>> tableEle;

    private static ObservableList<LinkedHashMap<String,Object>> dataList = FXCollections.observableArrayList();

    private static Boolean flag = true;

    @FXML
    private Text countEle;

    private Log log = LogFactory.get();


    @Override
    public void initialize(URL location, ResourceBundle resources) {
        tableEle.setVisible(false);
        tableEle.setItems(dataList);
        sqlEle.setOnKeyPressed(event -> {
            if(event.getCode() == KeyCode.ENTER){
                execute();
            }
        });
    }

    @FXRedirect
    public String jumpFunction(){
        return "FunctionController";
    }

    public void execute(){
        String sql = sqlEle.getText();
        if(StringUtil.isEmpty(sql)){
            return;
        }
        String [] sqls = sql.split(";");
        SqlMapper sqlMapper = MybatisUtils.getMapper(SqlMapper.class);
        StringBuilder strBuf = new StringBuilder();
        try{
            if(sqls.length == 1){
                dataList.clear();
                tableEle.getColumns().clear();
                List<LinkedHashMap<String,Object>> result = sqlMapper.customQueryItem(sql);
                dataList.addAll(result);
                LinkedHashMap<String,Object> map = dataList.get(0);
                for(String key:map.keySet()){
                    ToolUtil.addTableColumns(key,tableEle);
                }
                countEle.setText(dataList.size()+"");
            }
        }catch (Exception e){
            log.error(e);
        }
        for(String content:sqls){
            try{
                List<LinkedHashMap<String,Object>> result = sqlMapper.customQueryItem(content);
                strBuf.append(content).append("=====").append(result.toString());
            }catch (Exception e){
                strBuf.append(content).append("=====").append(e.toString());
            }
        }
        resultEle.setText(strBuf.toString());
    }


    @FXML
    public void changeShowType(){
        tableEle.setVisible(flag);
        resultEle.setVisible(!flag);
        flag = !flag;
    }

}
